package com.sylar.happyreader;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.app.ActivityGroup;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

import com.sylar.happyreader.adapter.BookGridAdapter;
import com.sylar.happyreader.bean.BookItem;
import com.sylar.happyreader.listener.Callback;
import com.sylar.happyreader.net.HttpTool;
import com.sylar.happyreader.tools.Config;
import com.sylar.happyreader.tools.TaskUtils;
import com.sylar.happyreader.tools.Utils;
import com.sylar.happyreader.view.ShelvesView;

public class BookGridActivity extends ActivityGroup implements OnItemClickListener, Callable<String>, Callback<String> {
	private ShelvesView mBookGrid;
	private List<BookItem> books;
	private BookGridAdapter bookAdapter;
	private BookItem selectBook;
	private ArrayList<View> history;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.history = new ArrayList<View>();

		replaceView(getLayoutInflater().inflate(R.layout.bookgrid, null));

		books = (List<BookItem>) this.getIntent().getSerializableExtra("books");

		mBookGrid = (ShelvesView) findViewById(R.id.grid_shelves);
		mBookGrid.setFastScrollEnabled(true);
		mBookGrid.setOnItemClickListener(this);

		if (books != null && mBookGrid != null) {
			bookAdapter = new BookGridAdapter(this, books);
			mBookGrid.setAdapter(bookAdapter);
		}

	}

	public void replaceView(View v) {
		if (history != null)
			history.add(v);
		setContentView(v);
	}

	public void back() {
		if (history == null)
			return;
		if (history.size() > 1) {
			history.remove(history.size() - 1);
			View v = history.get(history.size() - 1);
			setContentView(v);
		} else {
			finish();
		}
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
			if (event.getAction() == KeyEvent.ACTION_DOWN && event.getRepeatCount() == 0) {
				back();
			}
			return true;
		} else {
			return super.dispatchKeyEvent(event);
		}

	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			back();
			return true;
		} else
			return super.onKeyDown(keyCode, event);// 除了捕获你想要的返回键之外，其余的应该交给他的父类去处理
	}

	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		selectBook = bookAdapter.getItem(position);
		TaskUtils.doAsync(this.getParent(), null, null, this, this);
	}

	public void onCallback(String pCallbackValue) {
		if (pCallbackValue != null && !"".equals(pCallbackValue)) {
			Config.pagelists = Utils.SubArray(pCallbackValue.split(".txt"));
			Intent i = new Intent(this, BookPagesActivity.class);
			i.putExtra("sex", selectBook.getSex());
			i.putExtra("style", selectBook.getStyle());
			i.putExtra("bookName", selectBook.getBookname());
			View v = getLocalActivityManager().startActivity(BookPagesActivity.class.getName(), i).getDecorView();
			setContentView(v);
		} else {
			Toast.makeText(this, "加载失败了哦", Toast.LENGTH_SHORT).show();
		}
	}

	public String call() throws Exception {
		if (selectBook == null)
			return null;
		String url = Config.RequestUri + "sex=" + selectBook.getSex() + "&style=" + Uri.encode(selectBook.getStyle()) + "&bookName=" + Uri.encode(selectBook.getBookname());
		String httpResult = HttpTool.getString(getApplicationContext(), url);
		return httpResult;
	}
}
